package cn.vshare.service.impl;

import cn.vshare.dao.MessageDao;
import cn.vshare.dao.UserDao;
import cn.vshare.entity.User;
import cn.vshare.service.MessageService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.log4j.Logger;
import org.apache.log4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("messageService")
public class MessageServiceImpl implements MessageService {
    private final Integer IS_FORBIDDEN = 0; //被禁用状态值为0
    private final Integer NON_FORBIDDEN = 1; //未被禁用状态值为1
    private final Integer IS_DESTORY = 5;  //被删除禁用状态值为5
    private final Integer NON_DESTORY = 0; //未被删除用状态值0

    @Autowired
    private MessageDao messageDao;
    @Autowired
    private UserDao userDao;
    private Logger log = Logger.getLogger(MessageServiceImpl.class); //引入日志类

    public boolean changeUserState() {
        Map map = new HashMap();
        map.put("state", 0);
        map.put("id", "0c527243-9949-4248-8f4b-ce1e9641c80a");
        try {
            MDC.put("id", "62256092-8d3b-4986-9e1b-e0dd36d470e3");
            MDC.put("uid", "0c527243-9949-4248-8f4b-ce1e9641c80a");
            MDC.put("ip", InetAddress.getLocalHost().getHostAddress());
            log.info("测试日志");
            return userDao.changeUserState(map);
        } catch (UnknownHostException e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 获取会员信息
     *
     * @param keyword 查询关键字
     * @return
     */
    @Override
    public Page<User> getAllMemberUser(int currPage, int pageSize, String keyword, String isForbidden, String isDestory) {
        Page<User> userPage = new Page<User>();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("name", keyword);
        if (isForbidden == null) {
            map.put("isForbidden", null);
        } else {
            if (isForbidden.equals("禁用")) {
                map.put("isForbidden", IS_FORBIDDEN);
            }else {
                map.put("isForbidden", NON_FORBIDDEN);
            }
        }
        if (isDestory == null){
            map.put("isDestory", null);
        }else {
            if (isDestory.equals("删除")) {
                map.put("isDestory", IS_DESTORY);
            }
            if (isDestory.equals("未删除")){
                map.put("isDestory", NON_DESTORY);
            }
        }

        int totalRows = userDao.countAllMemberUser(map); //获取总条数
        int totalPages = totalRows % pageSize == 0 ? totalRows / pageSize : totalRows / pageSize + 1; //获取总页数
        //设置分页属性
        userPage.setTotal(totalRows);
        userPage.setPageNum(totalPages);
        userPage.setPageSize(pageSize);
        userPage = PageHelper.startPage(currPage, pageSize); //执行分页
        List<User> userList = userDao.getAllMemberUser(map);
        return userPage;
    }

    @Override
    public Page<User> getMemberByKeyword(String startTime, String endTime, String keyword, int currPage, int pageSize, String condition, String isForbidden, String isDestory) {
        Page<User> userPage = new Page<User>(); //新建分页对象

        //添加查询条件
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("startTime", startTime);
        map.put("endTime", endTime);
        map.put("keyword", keyword);
        map.put(condition, condition);
        if (isForbidden.equals("禁用")) {  //若用户是被禁用则查询状态值为1
            map.put("isForbidden", NON_FORBIDDEN);
        } else {
            map.put("isForbidden", IS_FORBIDDEN);
        }
        if (isDestory.equals("删除")) {  //若用户被删除,则查询状态为5
            map.put("isDestory", IS_DESTORY);
        }
        int totalRows = userDao.countMemberUserByTimeAndKeyword(map); //获取总行数
        int totalPages = totalRows % pageSize == 0 ? totalRows / pageSize : totalRows / pageSize + 1;
        userPage.setTotal(totalRows);
        userPage.setPageNum(totalPages);
        userPage.setPageSize(pageSize);
        userPage = PageHelper.startPage(currPage, pageSize);
        List<User> userList = userDao.getMemberUserByTimeAndKeyword(map);
        return userPage;
    }

    public MessageDao getMessageDao() {
        return messageDao;
    }

    public void setMessageDao(MessageDao messageDao) {
        this.messageDao = messageDao;
    }

    public UserDao getUserDao() {
        return userDao;
    }

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

}
